<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
    integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>

<body>
  <div id="app">
    <div class="container">
      <h1 class="text-center text-primary">信息输入</h1>
      <form>
        <div class="form-group">
          <label for="exampleInputEmail1">姓名</label>
          <input type="text" v-model="name" class="form-control" id="exampleInputEmail1" placeholder="姓名">
        </div>
        <div class="form-group">
          <label for="exampleInputPassword1">手机号</label>
          <input type="text" v-model="phone" class="form-control" id="exampleInputPassword1" placeholder="手机号">
        </div>
        <div class="form-group">
          <label for="info">备注信息</label>
          <input type="text" v-model="info" class="form-control" id="info" placeholder="备注信息">
        </div>
        <button type="button" class="btn btn-primary" @click="add">提交</button>
        <button type="reset" class="btn btn-info">重置</button>
      </form>
      <h1 class="text-center text-primary">学员信息展示</h1>
      <table class="table table-bordered">
        <thead>
          <tr>
            <th>姓名</th>
            <th>手机号</th>
            <th>备注信息</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(stu, index) in list" :key="index">
            <td>{{stu.name}}</td>
            <td>{{stu.phone}}</td>
            <td>{{stu.info}}</td>
            <td>
              <button class="btn btn-danger" @click="del(index)">删除</button>
            </td>
          </tr>
          <tr v-if="list.length === 0">
            <td colspan="4">暂无数据</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
  <script src="../vue.js"></script>
  <script>
    let vm = new Vue({
      el: '#app',
      data: {
        name: '',
        phone: '',
        info: '',
        // 学员信息组成的数组
        list: []
      },
      created() {
        this.list = JSON.parse(localStorage.getItem('list')) || []
      },
      methods: {
        add() {
          // name和phone是必填项
          if(this.name && this.phone) {
            this.list.push({
              name: this.name,
              phone: this.phone,
              info: this.info
            })
          }
          // 本地存储
          localStorage.setItem('list', JSON.stringify(this.list))
          // console.log(this.list);
          // 清空输入框
          this.name = '';
          this.phone = '';
          this.info = '';
        },
        del(index) {
          // 按照index删除
          this.list.splice(index, 1);
          // 本地存储
          localStorage.setItem('list', JSON.stringify(this.list))
        }
      },
    })
  </script>
</body>

</html>